<?php
namespace app\facade;

use think\Facade;

class HttpProxy extends Facade
{
    public static function set_header($cookie) {
        return array(
            'Host: '.self::$host,
            'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.1',
            'Origin: '.self::$host,
            'Referer: '.self::$host,
            'Content-Type: application/x-www-form-urlencoded',
            'cookie: '.$cookie,
        );
    }

    protected static $host = "";
    public static function set_host($host) {
        self::$host = $host;
    }

    public static function http_post($url, $post_data,$cookie="", $get_header=false) {
        $postdata = http_build_query($post_data);
        $options = array(
            'http' => array(
                'method'        => 'POST',
                'header'        => self::set_header($cookie),
                'content'       => $postdata,
            )
        );
        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
        //var_dump($http_response_header);
        if($get_header) {
            // 需要返回header
            return array(
                'header'    => $http_response_header,
                'body'      => $result,
            );
        } else {
            return $result;
        }
    }
    public static function http_get($url, $get_data,$cookie="",$get_header = false) {
        $options = array(
            'http' => array(
                'method'        => 'GET',
                'header'        => self::set_header($cookie),
            )
        );
        $context = stream_context_create($options);
        $url .= '?'.http_build_query($get_data);
        $result = file_get_contents($url, false);
        //var_dump($http_response_header);
        if($get_header) {
            // 需要返回header
            return array(
                'header'    => $http_response_header,
                'body'      => $result,
            );
        } else {
            return $result;
        }
    }
}
/**
 * 请求头设置
 */
function set_json_header($cookie) {
    return array(
        'Host: zhxg.njtech.edu.cn',
        'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.1',
        'Origin: http://zhxg.njtech.edu.cn',
        'Referer: http://zhxg.njtech.edu.cn',
        'Content-Type: application/json',
        'cookie: '.$cookie,
    );
}


function getWEU($str) {
    $b = mb_strpos($str,'_WEU=') + mb_strlen('_WEU=');
    $e = mb_strpos($str,'Path=/xsfw/') - $b;
    return mb_substr($str,$b,$e);
}

function json_post($url, $post_data,$get_header=false,$cookie) {
    $postdata = json_encode($post_data);
    $options = array(
        'http' => array(
            'method'        => 'POST',
            'header'        => set_json_header($cookie),
            'content'       => $postdata,
        )
    );
    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    //var_dump($http_response_header);
    if($get_header) {
        // 需要返回header
        return array(
            'header'    => $http_response_header,
            'body'      => $result,
        );
    } else {
        return $result;
    }
}